package net.xju.service.impl;

import net.xju.dao.UserDao;
import net.xju.entity.User;
import net.xju.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDao();

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (checkUsernameExists(user.getUsername())) {
            return false;
        }
        // 检查邮箱是否已存在
        if (checkEmailExists(user.getEmail())) {
            return false;
        }
        // 注册用户
        int rows = userDao.insert(user);
        return rows > 0;
    }

    @Override
    public User login(String username, String password) {
        User user = userDao.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public boolean deleteUser(int userId) {
        int rows = userDao.deleteById(userId);
        return rows > 0;
    }

    @Override
    public boolean checkUsernameExists(String username) {
        User user = userDao.findByUsername(username);
        return user != null;
    }

    @Override
    public boolean checkEmailExists(String email) {
        // 此处需添加根据邮箱查询用户的方法（类似findByUsername）
        return false; // 简化实现
    }
}